package zhoug.zui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;

import java.text.MessageFormat;

import zhoug.common.utils.DrawableUtils;
import zhoug.logging.Logger;
import zhoug.common.utils.SpannableUtils;

import androidx.annotation.Nullable;
import zhoug.zui.utils.ZUIUtils;

/**
 * 可以设置drawable大小
 *
 * @Author 35574
 * @Date 2020/9/4
 * @Description
 */
public class ZUITextView extends androidx.appcompat.widget.AppCompatTextView {
    private static final String TAG = ">>>ZUITextView";
    private static boolean debug = false;
    private int drawableWidth = 0;
    private int drawableHeight = 0;
    private boolean required = false;
    private boolean unRequiredTag = false;
    private boolean appendColon = false;//是否添加冒号
    private int requiredTagGravity;//星号标记的位置 必须
    private int alignTextLength;//对齐多少个文字
    private CharSequence text;
    public static final int START = 1;
    public static final int END = 2;

    public static void setDebug(boolean debug) {
        ZUITextView.debug = debug;
    }

    public ZUITextView(Context context) {
        this(context, null);

    }

    public ZUITextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public ZUITextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        parseAttr(context, attrs, defStyleAttr, 0);
    }

    private void parseAttr(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZUITextView, defStyleAttr, defStyleRes);
        drawableWidth = a.getDimensionPixelOffset(R.styleable.ZUITextView_zui_drawable_width, 0);
        drawableHeight = a.getDimensionPixelOffset(R.styleable.ZUITextView_zui_drawable_height, 0);
        required = a.getBoolean(R.styleable.ZUITextView_zui_required, false);
        unRequiredTag = a.getBoolean(R.styleable.ZUITextView_zui_unRequiredTag, false);
        appendColon = a.getBoolean(R.styleable.ZUITextView_zui_appendColon, false);
        alignTextLength = a.getInt(R.styleable.ZUITextView_zui_alignTextLength, 0);
        requiredTagGravity = a.getInt(R.styleable.ZUITextView_zui_requiredTagGravity, Gravity.LEFT);
        text = a.getString(R.styleable.ZUITextView_android_text);
        a.recycle();
        invalidateCompoundDrawables();
        setRequired(required);
    }


    /**
     * Drawable尺寸改变后更新CompoundDrawables
     */
    private void invalidateCompoundDrawables() {
        if (drawableWidth > 0 || drawableHeight > 0) {
            Drawable[] compoundDrawables = getCompoundDrawables();
            Drawable left = compoundDrawables[0];
            Drawable top = compoundDrawables[1];
            Drawable right = compoundDrawables[2];
            Drawable bottom = compoundDrawables[3];
            Drawable[] compoundDrawablesRelative = getCompoundDrawablesRelative();
            Drawable start = compoundDrawablesRelative[0];
            Drawable end = compoundDrawablesRelative[2];
            if (left != null || top != null || right != null || bottom != null) {
                setCompoundDrawables(left, top, right, bottom);
            }
            if (start != null || end != null) {
                setCompoundDrawablesRelative(start, top, end, bottom);
            }
        }
    }

    /**
     * 设置Drawable大小
     *
     * @param drawable
     */
    private void setCompoundDrawableSize(Drawable drawable) {
        if (drawableWidth > 0 || drawableHeight > 0) {
            DrawableUtils.setDrawableSize(drawable, drawableWidth, drawableHeight);
        }
    }

    /**
     * 设置drawable的大小
     *
     * @param width
     * @param height
     */
    public void setDrawableSize(int width, int height) {
        this.drawableWidth = width;
        this.drawableHeight = height;
        invalidateCompoundDrawables();
    }

    /**
     * 获取左边的Drawable
     *
     * @return
     */
    private Drawable getDrawableLeft() {
        return getCompoundDrawables()[0];
    }

    /**
     * 获取顶部的Drawable
     *
     * @return
     */
    private Drawable getDrawableTop() {
        return getCompoundDrawables()[1];
    }

    /**
     * 获取右边的Drawable
     *
     * @return
     */
    private Drawable getDrawableRight() {
        return getCompoundDrawables()[2];
    }

    /**
     * 获取底部的Drawable
     *
     * @return
     */
    private Drawable getDrawableBottom() {
        return getCompoundDrawables()[3];
    }

    /**
     * DrawableStart
     *
     * @return
     */
    private Drawable getDrawableStart() {
        return getCompoundDrawablesRelative()[0];
    }

    /**
     * DrawableEnd
     *
     * @return
     */
    private Drawable getDrawableEnd() {
        return getCompoundDrawablesRelative()[2];
    }

    @Override
    public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
        if (drawableWidth > 0 || drawableHeight > 0) {
            setCompoundDrawableSize(left);
            setCompoundDrawableSize(top);
            setCompoundDrawableSize(right);
            setCompoundDrawableSize(bottom);
        }

        if (debug) {
            Logger.d(TAG, "setCompoundDrawables:drawableWidth=" + drawableWidth + ",drawableHeight=" + drawableHeight);
            Logger.d(TAG, "setCompoundDrawables:left=" + left);
            Logger.d(TAG, "setCompoundDrawables:top=" + top);
            Logger.d(TAG, "setCompoundDrawables:right=" + right);
            Logger.d(TAG, "setCompoundDrawables:bottom=" + bottom);
        }
        super.setCompoundDrawables(left, top, right, bottom);
    }

    @Override
    public void setCompoundDrawablesRelative(@Nullable Drawable start, @Nullable Drawable top, @Nullable Drawable end, @Nullable Drawable bottom) {
        if (drawableWidth > 0 || drawableHeight > 0) {
            setCompoundDrawableSize(start);
            setCompoundDrawableSize(top);
            setCompoundDrawableSize(end);
            setCompoundDrawableSize(bottom);
        }
        if (debug) {
            Logger.d(TAG, "setCompoundDrawablesRelative:drawableWidth=" + drawableWidth + ",drawableHeight=" + drawableHeight);
            Logger.d(TAG, "setCompoundDrawablesRelative:start=" + start);
            Logger.d(TAG, "setCompoundDrawablesRelative:top=" + top);
            Logger.d(TAG, "setCompoundDrawablesRelative:end=" + end);
            Logger.d(TAG, "setCompoundDrawablesRelative:bottom=" + bottom);
        }
        super.setCompoundDrawablesRelative(start, top, end, bottom);
    }

    public boolean isRequired() {
        return required;
    }

    public void setUnRequiredTag(boolean unRequiredTag) {
        this.unRequiredTag = unRequiredTag;
    }

    public void setRequired(boolean required) {
        this.required = required;
        if (text != null) {
            notifyRequired();
        }
    }

    public void setAppendColon(boolean appendColon) {
        this.appendColon = appendColon;
        if (text != null) {
            notifyRequired();
        }
    }

    public void setAlignTextLength(int alignTextLength) {
        this.alignTextLength = alignTextLength;
        if (text != null) {
            notifyRequired();
        }
    }


    public void setText1(CharSequence text) {
        this.text = text;
        notifyRequired();
    }

    public CharSequence getText1() {
        return text;
    }

    /**
     * 设置星号位置
     *
     * @param requiredTagGravity {@link #START,#END}
     */
    public void setRequiredTagGravity(int requiredTagGravity) {
        if (requiredTagGravity == START) {
            this.requiredTagGravity = Gravity.START;
        } else {
            this.requiredTagGravity = Gravity.END;
        }
        if (text != null) {
            notifyRequired();
        }
    }


    protected void notifyRequired() {
        CharSequence name = text;
        if (alignTextLength > 0) {
            name = ZUIUtils.getAlignName(text, alignTextLength);
        }
        if (appendColon) {
            name = MessageFormat.format("{0}:", name != null ? name : "");
        }
        if (required) {
            super.setText(SpannableUtils.requiredText(name, requiredTagGravity));
        } else {
            if (unRequiredTag) {
                super.setText(SpannableUtils.unRequiredTag(name, requiredTagGravity));
            } else {
                super.setText(name);
            }
        }
    }

    private String getColonText() {
        return MessageFormat.format("{0}:", text != null ? text : "");
    }


}
